package com.service.impl;

import com.dao.BookDao;
import com.dao.impl.BookDaoImp;
import com.pojo.Book;
import com.pojo.Page;
import com.service.bookService;

import java.util.List;

public class bookServiceImpl implements bookService {
    BookDao bookDao = new BookDaoImp();
    @Override
    public void addBook(Book book) {
        bookDao.addBook(book);
    }

    @Override
    public void delBookById(int no) {
        bookDao.delBookById(no);
    }

    @Override
    public void updateBook(Book book) {
        bookDao.updateBook(book);
    }

    @Override
    public Book queryBookById(int no) {
        return bookDao.queryBookById(no);
    }

    @Override
    public List<Book> queryBooks() {
        return bookDao.queryBooks();
    }

    @Override
    public void deleteBookById(int id) {
        bookDao.delBookById(id);
    }


    @Override
        public Page<Book> pages(int pageno, int pagesize) {
        Page<Book> page = new Page<Book>();
        //设置当前页码
        page.setPageNo(pageno);
        //每页显示数量
        page.setPageSize(pagesize);
        Integer count = bookDao.queryForPageCount();
        //设置页面总记录数
        page.setPageTotalcount(count);
      Integer pageTotal =  count/pagesize;

      if(count%pagesize!=0){
          pageTotal++;
      }
      page.setPageTotal(pageTotal);
      //设置总页码
        page.setPageTotal(pageTotal);
      int begin = (page.getPageNo()-1)*pagesize;
      //设置当前页面数据
        System.out.println();
       page.setItems(bookDao.queryForCurrentPageData(begin,pagesize));
       return page;
    }

    @Override
    public Page<Book> pageByPrice(int max,int min,int pageNo,int pageSize) {
        Page<Book> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Integer count = bookDao.queryForPageTotalcount(min, max);
        page.setPageTotalcount(count);
        Integer pageTotal = count/pageSize;
        if(count%pageSize!=0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        int begin = (pageNo-1)*pageSize;
        page.setItems(bookDao.queryForPageItems(min,max,begin,pageSize));
        return page;
    }

}
